package com.example.blog.controller;

import com.example.blog.base.Result;
import com.example.blog.entity.DictionaryItem;
import com.example.blog.entity.dto.dictionaryGroup.AddOrUpdateDictGroupDTO;
import com.example.blog.service.DictionaryItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 字典模块
 * <p>
 * 字典项表 前端控制器
 * </p>
 *
 * @author wlh
 * @since 2024-01-10
 */
@RestController
@RequestMapping("/blog/dictionaryItem")
public class DictionaryItemController {

    
    @Autowired
    private DictionaryItemService dictionaryItemService;
    
    
    
    /**
     * 新增或修改字典
     * @param addOrUpdateDictGroupDTO
     * @return
     */
    @PostMapping("/add-or-update")
    public Result<Void> addOrUpdate(@RequestBody @Validated AddOrUpdateDictGroupDTO addOrUpdateDictGroupDTO) {
        dictionaryItemService.addOrUpdate(addOrUpdateDictGroupDTO);

        return Result.success();
    }

    /**
     * 删除字典
     * @param id 字典ID
     * @return
     */
    @DeleteMapping("/{id}")
    public Result<Void> del(@PathVariable Long id) {
        dictionaryItemService.removeById(id);
        return Result.success();
    }


    /**
     * 根据分组ID获取该组下的所有字典项
     * @param groupId 字典组ID
     * @return
     */
    @GetMapping("/list/{groupId}")
    public Result<List<DictionaryItem>> list(@PathVariable Long groupId) {
        List<DictionaryItem> list = dictionaryItemService.getByGroupId(groupId);
        return Result.successData(list);
    }
}
